﻿using System;
using RayDen.Library.Core;
using RayDen.RayEngine.Core.Interface;
using RayDen.RayEngine.Core.Types;

namespace RayDen.RayEngine.Samplers
{
    public class RandomSampler : ISampler
    {
        public static int MagicValue1 = 64;
        public static int MagicValue2 = 128;
        public int Width { get; protected set; }
        public int Height { get; protected set; }

        public int screenWidth, screenHeight, screenStartLine, samplePerPixel, samplePerPixel2;
        int currentSampleScreenX, currentSampleScreenY, currentSubSampleIndex;
        public int pass;
        public bool lowLatency, previewOver;
        public int valuesPerSample;

        private IRandomProvider rnd;

        public int tIdx;

        public RandomSampler()
        {
            rnd = 
               //new HaltonSequence();
             new FastRandom();
             //new BaseQMCSequence();
        }
        public void Reinitialise(int seed)
        {
            this.rnd.Reinitialise(seed);
        }

        public float GetLDValue(Sample sample)
        {
            return rnd.NextFloat();

        }

        public float NextFloat()
        {
            return rnd.NextFloat();
        }

        public double NextDouble()
        {
            return rnd.NextDouble();
        }

        public Sample GetSample(Sample prevSample)
        {
            Sample sm;
            if (!lowLatency || (pass >= MagicValue2))
            {
                previewOver = true;
                // In order to improve ray coherency
                if (samplePerPixel == 1)
                    sm = GetNextSample1x1(prevSample);
                else
                    sm = GetNextSampleNxN(prevSample);
            }
            else if (previewOver || (pass >= MagicValue1))
            {
                previewOver = true;
                sm = GetNextSampleNxN(prevSample);
            }
            else
            {
                // In order to update the screen faster for the first 16 passes
                sm = GetNextSamplePreview();
            }
            TotalSamples++;
            //sm.imageY += screenStartLine;
            return sm;
        }

        public float GetLazyValue(Sample sample)
        {
            return rnd.NextFloat();
        }

        public void Init(int width, int height)
        {
            currentSubSampleIndex = 0;
            pass = 0;
            previewOver = !lowLatency;
            samplePerPixel = 1;
            samplePerPixel2 = 4;
            if (screenWidth == 0 && screenHeight == 0)
            {
                Width = screenWidth = width;
                Height = screenHeight = height;
                screenWidth = width - 1;
                screenHeight = height - 1;
            }
            screenStartLine = Math.Max(0, screenStartLine - 1);
            currentSubSampleIndex = 0;
            currentSampleScreenY = screenStartLine;
            TotalSamples = 0;
            //startTime = WallClockTime(); 
        }

        public int GetPass()
        {
            return pass;
        }

        public long TotalSamples { get; set; }
        public float GetLazyValue(Sample sample, int index)
        {
            return rnd.NextFloat();
        }

        Sample GetNextSampleNxN(Sample oldSample)
        {
            int stepX = currentSubSampleIndex % samplePerPixel;
            int stepY = currentSubSampleIndex / samplePerPixel;

            int scrX = currentSampleScreenX;
            int scrY = currentSampleScreenY;

            currentSubSampleIndex++;
            if (currentSubSampleIndex == samplePerPixel2)
            {
                currentSubSampleIndex = 0;
                currentSampleScreenX++;
                if (currentSampleScreenX >= screenWidth)
                {
                    currentSampleScreenX = 0;
                    currentSampleScreenY++;

                    if (currentSampleScreenY >= screenHeight)
                    {
                        currentSampleScreenY = 0;
                        pass += samplePerPixel2;
                    }
                }
            }

            float r1 = (stepX + rnd.NextFloat()) / samplePerPixel - .5f;
            float r2 = (stepY + rnd.NextFloat()) / samplePerPixel - .5f;
            Sample sample = oldSample ?? new Sample(this) {};
            sample.imageX = scrX + r1;
            sample.imageY = scrY + r2;
            sample.pass = pass;

            return sample;

        }

        Sample GetNextSample1x1(Sample oldSample)
        {
            int scrX = currentSampleScreenX;
            int scrY = currentSampleScreenY;

            currentSampleScreenX++;
            if (currentSampleScreenX >= screenWidth)
            {
                currentSampleScreenX = 0;
                currentSampleScreenY++;

                if (currentSampleScreenY >= screenHeight)
                {
                    currentSampleScreenY = 0;
                    pass++;
                }
            }

            float r1 = rnd.NextFloat() - .5f;
            float r2 = rnd.NextFloat() - .5f;
            Sample sample = oldSample ?? new Sample(this) { };
            sample.imageX = scrX + r1;
            sample.imageY = scrY + r2;
            sample.pass = pass;
            return sample;
        }

        Sample GetNextSamplePreview()
        {
            int scrX, scrY;
            for (; ; )
            {
                int stepX = pass % 4;
                int stepY = (pass / 4) % 4;

                scrX = currentSampleScreenX * 4 + stepX;
                scrY = currentSampleScreenY * 4 + stepY;

                currentSampleScreenX++;
                if (currentSampleScreenX * 4 >= screenWidth)
                {
                    currentSampleScreenX = 0;
                    currentSampleScreenY++;

                    if (currentSampleScreenY * 4 >= screenHeight)
                    {
                        currentSampleScreenY = 0;
                        pass++;
                    }
                }

                // Check if we are inside the screen
                if ((scrX < screenWidth) && (scrY < screenHeight))
                {
                    // Ok, it is a valid sample
                    break;
                }
                else if (pass >= MagicValue1)
                {
                    return GetSample(null);
                }
            }
            float r1 = rnd.NextFloat() - .5f;
            float r2 = rnd.NextFloat() - .5f;

            var sample = new Sample(this) { imageX = scrX + r1, imageY = scrY + r2, pass = pass };
            return sample;
        }
    }
}